package cz.oplevka.lahvodroid;

import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLHandler extends DefaultHandler {

	Boolean currentElement = false;
	String currentValue = null;
	public static XMLBeerList xmlBeerList = null;   
        String barcode;

	public static XMLBeerList getXMLBeerList() {
		return xmlBeerList;
	}

	public static void setSitesList(XMLBeerList list) {
		XMLHandler.xmlBeerList = list;
	}

	/** Called when tag starts ( ex:- <name>AndroidPeople</name> 
	 * -- <name> )*/
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		currentElement = true;

		if (localName.equals("maintag"))
		{
			/** Start */ 
			xmlBeerList = new XMLBeerList();
		} else if (localName.equals("jmeno")) {
			/** Get attribute value */
			String attr = attributes.getValue("typ");
			xmlBeerList.setTyp(attr);
		}

	}

	/** Called when tag closing ( ex:- <name>AndroidPeople</name> 
	 * -- </name> )*/
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		
                currentElement = false; 

		/** set value */ 
		if (localName.equalsIgnoreCase("jmeno")){
			xmlBeerList.setJmeno(currentValue);
                        xmlBeerList.setZaznam(currentValue);
                        xmlBeerList.setJmeno2(currentValue);
                } else if (localName.equalsIgnoreCase("pivovar")){
                        xmlBeerList.setPivovar(currentValue);
                        xmlBeerList.setZaznam(currentValue);                        
                } else if (localName.equalsIgnoreCase("alkohol")){
                    xmlBeerList.setAlkohol(currentValue);
                    xmlBeerList.setZaznam(currentValue);
                } else if (localName.equalsIgnoreCase("poznamka")){
                    xmlBeerList.setPoznamka(currentValue);
                    xmlBeerList.setZaznam(currentValue);
                } else if (localName.equalsIgnoreCase("barcode")){
                    xmlBeerList.setBarcode(currentValue);                                       
                    barcode = currentValue;
                    ArrayList<String> temp = xmlBeerList.getZaznam();
                    xmlBeerList.putMap(barcode, temp);
                    xmlBeerList.putNameMap(xmlBeerList.getJmeno2(), barcode);
                    ArrayList<String> deleteZaznam = new ArrayList<String>();
                    xmlBeerList.setZaznamList(deleteZaznam);
                    String bla = "bla";
                }
                
                
                
                
			

	}

	/** Called to get tag characters ( ex:- <name>AndroidPeople</name> 
	 * -- to get AndroidPeople Character ) */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}

	}   

}
